package com.crux.jpa.version.service;

import com.crux.jpa.version.dao.UserDao;
import com.crux.jpa.version.dao.UserOfficeDao;
import com.crux.jpa.version.entity.TempUser;
import com.crux.jpa.version.entity.User;
import com.crux.jpa.version.entity.UserOffice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private EntityManager entityManager;
    private UserOfficeDao userOfficeDao;

    public UserServiceImpl(UserDao userDao, EntityManager entityManager, UserOfficeDao userOfficeDao) {
        this.userDao = userDao;
        this.entityManager = entityManager;
        this.userOfficeDao = userOfficeDao;
    }

    public User addUser(String name) {
        User user = new User();
        user.setName(name);
        userDao.save(user);
        return user;
    }

    @Transactional
    public void modify() {
        User userCond = new User();
        userCond.setName("test");
        User user = userDao.findOne(Example.of(userCond)).get();
        user.setName("test2");

        log.debug("---:{}", user);
        //List<User> users = userDao.findAll();
        Query query = entityManager.createQuery("from User");
        query.setLockMode(LockModeType.PESSIMISTIC_READ);

        List<User> users = query.getResultList();
        users.forEach(user1 -> log.debug("findAll:{}", user1));
    }

    @Override
    @Transactional
    public int batchInsert() {
        System.out.println("开始---------------");
        long start = System.currentTimeMillis();
        List<TempUser> tempUserList = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            TempUser tempUser = new TempUser();
            tempUser.setOrgId(1000L);
            tempUser.setSeqId(UUID.randomUUID().toString());
            tempUserList.add(tempUser);
            entityManager.persist(tempUser);
        }
        entityManager.flush();

        long endTime = System.currentTimeMillis();
        System.out.println(endTime - start);
        System.out.println("ed---------------");
        return tempUserList.size();
    }

    @Override
    public int test() {
        Query query = entityManager.createQuery("from UserOffice where id=822561175126162");
        List<UserOffice> list = query.getResultList();
        for (UserOffice userOffice : list) {
            System.out.println(userOffice.getOrganization());
        }
        return 0;
    }

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = null;

        try {
            request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        } catch (IllegalStateException var2) {
            log.warn("不是http请求,没有request");
        }

        return request;
    }



    @Override
    public int test2() {
        ThreadLocal<Integer> n = new InheritableThreadLocal<>();
        n.set(333);
        //ExecutorService pool = Executors.newCachedThreadPool();
        SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
        executor.execute(() -> System.out.println(n.get() + "----------1"+getRequest()));
        new Thread(() -> System.out.println(n.get() + "----------2"+getRequest())).start();
        return 0;
    }
}
